package com.final_project.zombiemenace;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Options extends Activity implements OnSeekBarChangeListener {
	private SeekBar volumeSeekbar = null;
	private AudioManager audioManager = null;
	static int currentMusicPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MainMenuActivity.continueBGMusic = true;
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		setContentView(R.layout.options);
		initControls();
		onClick();
	}

	private void onClick() {
		Button okButton = (Button) findViewById(R.id.button_ok);
		CheckBox checkboxMute = (CheckBox) findViewById(R.id.muteCheckBox);
		CheckBox checkboxVibrate = (CheckBox) findViewById(R.id.vibrationCheckBox);
		setOKButtonOnClickListener(okButton);
		setMuteCheckBoxOnCheckedChange(checkboxMute);
		setVibrateCheckBoxOnCheckedChange(checkboxVibrate);
	}

	private void setVibrateCheckBoxOnCheckedChange(
			final CheckBox checkboxVibrate) {
		checkboxVibrate
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (checkboxVibrate.isChecked()) {
							MainMenuActivity.vibration = true;
						} else {
							MainMenuActivity.vibration = false;
						}
					}

				});
	}

	private void setMuteCheckBoxOnCheckedChange(final CheckBox checkboxMute) {
		checkboxMute.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (checkboxMute.isChecked()) {
					audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0,
							0);
					MainMenuActivity.isMuted = true;
					MainMenuActivity.musicVolume = 0;
					volumeSeekbar
							.setProgress((int) MainMenuActivity.musicVolume);

				} else {
					MainMenuActivity.isMuted = false;
					audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
							(int) MainMenuActivity.musicVolume, 0);
				}
			}
		});
	}

	private void setOKButtonOnClickListener(Button okButton) {
		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initControls() {
		CheckBox muteCheckbox = (CheckBox) findViewById(R.id.muteCheckBox);
		CheckBox vibrationCheckbox = (CheckBox) findViewById(R.id.vibrationCheckBox);
		initSeekbar(muteCheckbox);
		if (MainMenuActivity.musicVolume == 0) {
			MainMenuActivity.isMuted = true;
		} else {
			MainMenuActivity.isMuted = false;
		}
		muteCheckbox.setChecked(MainMenuActivity.isMuted);
		vibrationCheckbox.setChecked(MainMenuActivity.vibration);
	}

	private void initSeekbar(final CheckBox muteCheckbox) {
		try {
			volumeSeekbar = (SeekBar) findViewById(R.id.soundSeekBar);
			audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			volumeSeekbar.getProgressDrawable().setColorFilter(Color.GREEN,
					Mode.SRC_IN);
			volumeSeekbar.setMax(audioManager
					.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
			audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
					(int) MainMenuActivity.musicVolume, 0);
			volumeSeekbar.setProgress((int) MainMenuActivity.musicVolume);

			volumeSeekbar
					.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
						@Override
						public void onStopTrackingTouch(SeekBar arg0) {
						}

						@Override
						public void onStartTrackingTouch(SeekBar arg0) {
						}

						@Override
						public void onProgressChanged(SeekBar arg0,
								int progress, boolean arg2) {
							audioManager.setStreamVolume(
									AudioManager.STREAM_MUSIC, progress, 0);
							if (progress != 0) {
								MainMenuActivity.isMuted = false;
								muteCheckbox
										.setChecked(MainMenuActivity.isMuted);
							}
							MainMenuActivity.musicVolume = progress;

						}
					});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
			int index = volumeSeekbar.getProgress();
			volumeSeekbar.setProgress(index + 1);
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
			int index = volumeSeekbar.getProgress();
			volumeSeekbar.setProgress(index - 1);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void onPause() {
		super.onPause();
		if (!MainMenuActivity.continueBGMusic)
			MusicManager.pause();
	}

	public void onResume() {
		super.onResume();

		MainMenuActivity.continueBGMusic = false;
		MusicManager.start(this, R.raw.dst_the_haunted_chapel);
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
	}
}
